package edu.nf.ch01.controller;

import edu.nf.ch01.common.MinioUtils;
import edu.nf.ch01.vo.ResultVO;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author wangl
 * @date 2025/9/4
 */
@RestController
public class UploadController extends BaseController{

    @PostMapping("/upload/{path}")
    public ResultVO  upload(@PathVariable("path") String path,
                            @RequestParam("file") MultipartFile[] files) throws Exception{
        //创建桶
        if(!MinioUtils.hasBucket("myapp")) {
            MinioUtils.createBucket("myapp");
        }
        for(MultipartFile file : files) {
            //获取文件名
            String fileName = file.getOriginalFilename();
            //获取文件输入流
            InputStream input = file.getInputStream();
            //获取文件类型
            String contentType = file.getContentType();
            //获取文件大小
            long size = file.getSize();
            //上传文件到minio文件服务器
            MinioUtils.upload("myapp", path + "/" + fileName, contentType, size, input);
        }
        return success();
    }
}